from datetime import datetime
import os
import time

from PIL import Image
from django.core.paginator import Paginator
from django.http import HttpResponse
from django.shortcuts import render
from django.db.models import Q

from common.models import Category,Shop

def index(request,pIndex):
    '''浏览菜品信息'''
    umod = Category.objects

    mywhere = []
    # 获取、判断并封装关keyword键搜索
    kw = request.GET.get("keyword", None)

    if kw:
        # 取出商铺id列表shopids准确匹配和菜品类别名称模糊匹配
        shopids = Shop.objects.filter(Q(name__contains=kw)).values_list('id', flat=True)
        list = umod.filter(Q(shop_id__in=shopids)|Q(name__contains=kw))
        mywhere.append("keyword=" + kw)
    else:
        list = umod.filter()

    # 分页逻辑
    if pIndex == '':
        pIndex = '1'
    pIndex = int(pIndex)
    p = Paginator(list,8)
    maxpages = p.num_pages  # 最大页数

    # 判断页数是否越界
    if pIndex > maxpages:
        pIndex = maxpages
    if pIndex < 1:
        pIndex = 1

    list2 = p.page(pIndex)
    plist = p.page_range

    # 遍历菜单类别信息，并获取对应的店铺名称，以shopname名封装
    for vo in list2:
        shop = Shop.objects.get(id=vo.shop_id)
        vo.shopname = shop.name

    context = {'categoryList': list2, 'plist': plist, 'pIndex': pIndex, 'maxpages': maxpages, 'mywhere': mywhere}
    return render(request,"myadmin/category/index.html",context)

def add(request):
    '''添加信息'''
    slist = Shop.objects.all()
    context ={'shopList':slist}
    return render(request,"myadmin/category/add.html",context)


def insert(request):
    '''执行增加'''
    try:
        # 获取菜品类别信息并执行添加
        ob = Category()
        ob.shop_id = request.POST['shop_id']
        ob.name = request.POST['name']
        ob.status = 1
        ob.create_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.update_at = ob.create_at
        ob.save()
        context = {'info': '添加成功！'}
    except Exception as err:
        print(err)
        context = {'info': '添加失败！'}
    return render(request, "myadmin/info.html", context)

def edit(request,cid):
    '''加载编辑页面信息'''
    try:
        ob = Category.objects.get(id=cid)
        # 查询出商品列表
        slist = Shop.objects.all()
        context={"category":ob,'shoplist':slist}
        return render(request,"myadmin/category/edit.html",context)
    except Exception as err:
        print(err)
        context = {"info":"没有找到菜品类别信息！"}
        return render(request,"myadmin/info.html",context)

def update(request,cid):
    '''执行编辑信息'''
    try:
        ob = Category.objects.get(id = cid)
        ob.shop_id = request.POST['shop_id']
        ob.name = request.POST['name']
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {"info":"菜品类别修改成功！"}
    except Exception as err:
        print(err)
        context = {"info":"菜品类别修改失败！"}
    return render(request,"myadmin/info.html",context)

def delete(request,cid):
    '''加载编辑页面信息'''
    try:
        ob = Category.objects.get(id=cid)
        # 查询出商品列表
        ob.shopname = Shop.objects.get(id=ob.shop_id).name
        print(ob.shopname)
        context={"category":ob}
        return render(request,"myadmin/category/edits.html",context)
    except Exception as err:
        print(err)
        context = {"info":"没有找到菜品类别信息！"}
        return render(request,"myadmin/info.html",context)

def updates(request,cid):
    '''执行编辑信息'''
    try:
        ob = Category.objects.get(id = cid)
        ob.status = request.POST['status']
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {"info":"菜品类别状态维护成功！"}
    except Exception as err:
        print(err)
        context = {"info":"菜品类别状态维护失败！"}
    return render(request,"myadmin/info.html",context)
